package com.chookss.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.aliyun.common.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chookss.R;
import com.chookss.tools.glideCorner.CornerLeftBottomTransform;
import com.chookss.tools.glideCorner.CornerLeftTopTransform;
import com.chookss.tools.glideCorner.CornerLeftTransform;
import com.chookss.tools.glideCorner.CornerRightBottomTransform;
import com.chookss.tools.glideCorner.CornerRightTopTransform;
import com.chookss.tools.glideCorner.CornerRightTransform;
import com.chookss.tools.glideCorner.CornerTopTransform;

/**
 * @author sunwei
 * 邮箱：tianmu19@gmail.com
 * 时间：2018/7/24 17:34
 * 包名：com.youtu.baselibrary.utils.glideu
 * <p>description:  glide加载类          </p>
 */

public class GlideUtils {


    private static RequestOptions options_square;
    private static RequestOptions car_options;

    private GlideUtils() {
        throw new UnsupportedOperationException("can not init ");
    }


    /**
     * 加载图片自定义 属性
     *
     * @param context
     * @param imgUrl
     * @param img
     * @param roundTransformDp 圆角矩形的边
     */
    public static void loadCustomRound(Context context, String imgUrl, ImageView img, int roundTransformDp) {
        if (null == context || null == img) return;
        RequestOptions options = new RequestOptions().error(R.drawable.default_news)
                .placeholder(R.drawable.default_news)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        if (0 != roundTransformDp)
            options.transform(new GlideRoundTransform(context, roundTransformDp)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).load(imgUrl)
                .apply(options)
                .into(img);
    }

    //专门针对图库自定义圆角
    public static void loadCustomRound2(Context context, String imgUrl, ImageView img, int type) {
        if (null == context || null == img) return;
        RequestOptions options = new RequestOptions().error(R.drawable.default_news)
                .placeholder(R.drawable.default_news)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        //0左上1右上2左下3 右下 4左边 5右边6上边
        if (type == 0) {
            options.transform(new CornerLeftTopTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else if (type == 1) {
            options.transform(new CornerRightTopTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else if (type == 2) {
            options.transform(new CornerLeftBottomTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else if (type == 3) {
            options.transform(new CornerRightBottomTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else if (type == 4) {
            options.transform(new CornerLeftTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else if (type == 5) {
            options.transform(new CornerRightTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else if (type == 6) {
            options.transform(new CornerTopTransform(context)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }

        Glide.with(context).load(imgUrl)
                .apply(options)
                .into(img);
    }

    /**
     * 加载图片自定义 属性
     *
     * @param context
     * @param imgUrl
     * @param img
     * @param roundTransformDp 圆角矩形的边
     */
    public static void loadCustomRoundGif(Context context, String imgUrl, ImageView img, int roundTransformDp) {
        if (null == context || null == img) return;
        RequestOptions options = new RequestOptions().error(R.drawable.default_news)
                .placeholder(R.drawable.default_news)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        if (0 != roundTransformDp)
            options.transform(new GlideRoundTransform(context, roundTransformDp)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).asGif().load(imgUrl)
                .apply(options)
                .into(img);
    }

    /**
     * 自定义默认图片
     *
     * @param context
     * @param imgUrl
     * @param img
     * @param roundTransformDp 圆角矩形的边
     */
    public static void loadCustomRound(Context context, String imgUrl, ImageView img, int defaultImg, int roundTransformDp) {
        if (null == context || null == img) return;
        RequestOptions options = new RequestOptions().error(defaultImg)
                .placeholder(defaultImg).dontAnimate().diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        if (0 != roundTransformDp)
            options.transform(new GlideRoundTransform(context, roundTransformDp)).encodeQuality(90).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).load(imgUrl)
                .apply(options)
                .into(img);
    }

    public static void loadAvatar(Context context, String imgUrl, ImageView avatarImg) {
        if (null == context) return;
        RequestOptions options = new RequestOptions()
                .error(R.drawable.default_user_head)
                .placeholder(R.drawable.default_user_head)
                .dontAnimate()
                .override(440, 440)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        if (null == avatarImg) return;
        if (StringUtils.isEmpty(imgUrl))
            Glide.with(context).load(R.drawable.default_user_head)
                    .apply(options)
                    .thumbnail(0.4f)
                    .into(avatarImg);
        else Glide.with(context).load(imgUrl)
                .apply(options)
                .thumbnail(0.4f)
                .into(avatarImg);
    }

    public static void load(Context context, String imgUrl, ImageView img, int placeholde) {
        if (null == context) return;

        if (null == img) return;
        RequestOptions options = new RequestOptions()
                .placeholder(placeholde)
                .error(placeholde)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        if (Utils.isNull(imgUrl)) {
            Glide.with(context).load(placeholde)
                    .into(img);
        } else {
            Glide.with(context).load(imgUrl)
                    .apply(options)
                    .into(img);
        }

    }

    public static void loadCenterCrop(Context context, String imgUrl, ImageView img, int placeholde) {
        if (null == context) return;

        if (null == img) return;
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholde)
                .error(placeholde)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        if (Utils.isNull(imgUrl)) {
            Glide.with(context).load(placeholde)
                    .into(img);
        } else {
            Glide.with(context).load(imgUrl)
                    .apply(options)
                    .into(img);
        }

    }

    public static Bitmap loadLogo(Context context) {
        if (context.getPackageName().equals("com.zhaofengnum")) {
            return BitmapFactory.decodeResource(context.getResources(), R.mipmap.zhaofeng_logo);
        } else {
            return BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo3);
        }
    }

    public static int loadLogoId(Context context) {
        if (context.getPackageName().equals("com.zhaofengnum")) {
            return R.mipmap.zhaofeng_logo;
        } else {
            return R.mipmap.logo3;
        }

    }
    public static int loadPushId(Context context) {
        if (context.getPackageName().equals("com.zhaofengnum")) {
            return R.drawable.zhaofeng_push;
        } else {
            return R.drawable.push;
        }

    }
}
